<template>
  <HHeader theme="white" style="box-shadow: rgba(0, 0, 0, 0.1) 1px 1px 1px 1px;">
    <Row>
      <Cell width="1">
        <Button icon="h-icon-menu" size="l" noBorder @click="siderChange"></Button>
      </Cell>
      <Cell width="13">
        <Select :datas="searchData" :filterable="true" @change="change" no-border align="left" placeholder="全局搜索"
          :deletable="false"></Select>
      </Cell>
      <Cell width="10" align="right">
        <Space :width="20" />
        <Icon type="h-icon-bell" @click="logOpend" isLink :size="15" />
        <Space :width="20" />
        <DropdownMenu @clickItem="trigger" :datas="param" class-name="h-text-dropdown" :width="150">
          <span>{{ loginName }}</span>
        </DropdownMenu>
      </Cell>
    </Row>
  </HHeader>
</template>

<script>
  import websocketStore from '@/store/websocket';
  import { confirm } from 'heyui';

  export default {
    computed: {
      siderCollapsed () {
        return this.$store.menus.siderCollapsed;
      },
      loginName () {
        return this.$store.loginInfo.loginName;
      },
      searchData () {
        return this.$store.menus.searchData;
      }
    },
    data () {
      return {
        wsStore: websocketStore(),
        datas: [],
        val: "",
        opened: false,
        page: {
          current: 1,
          size: 10,
        },
        param: [
          { label: "修改密码", value: "password", icon: "h-icon-user" },
          { divider: true },
          {
            label: "退出登录",
            value: "logout",
            icon: "h-icon-info",
          },
        ],
        fileLog: "",
      };
    },
    methods: {
      logOpend () {
        this.opened = true;
        this.getData();
      },
      getData () {
        this.$post("/buildLog/page", {
          page: this.page,
        }).then(res => {
          if (res.data == null) {
            this.datas = [];
            this.page.total = 0;
            return;
          }
          if (res.data.code != this.$success && res.msg != null) {
            message.error(res.msg);
            return;
          }
          if (!!res.data.data) {
            if (res.data.data.rows != null) {
              this.datas = res.data.data.rows;
            } else {
              this.datas = [];
            }
            this.page.total = res.data.data.total;
            return;
          } else {
            this.datas = [];
            this.page.total = 0;
          }
        });
      },
      logDelete (data) {
        this.$delete("/buildLog/" + data.id).then(res => {
          this.getData();
        });
      },
      logInfo (data) {
        this.$get("/buildLog/getLogFile/" + data.id).then(res => {
          this.opened = false;
          this.fileLog = res.data.data;
          this.fileOpened = true;
        });
      },
      fileLogClose () {
        this.fileOpened = false;
        this.fileLog = "";
        this.opened = true;
      },
      clearHis () {
        this.$delete("/buildLog/deleteHis").then(res => {
          this.getData();
        });
      },
      trigger (code, data, event) {
        if (code == "password") {
          this.$router.push("/sysusers/passwd");
        } else if (code == "logout") {
          confirm({
            content: '确定注销登录？',
            onConfirm: () => {
              this.$store.setLoginFlag(false);
              this.$store.setMenuData([]);
              const message = { type: 'notification', content: 'logout' };
              window.parent.postMessage(message, '*');
            }
          });
        } 
      },
      change (data) {
        if (!data) {
          return;
        }
        this.$store.setCurrent(data.value);
        this.$store.setTitle(data.label);
        this.$router.push(data.value);
      },
      siderChange () {
        this.$store.setSiderCollapsed();
      },
    },
  };
</script>